﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;

namespace Quebec.Silverlight.Extentions
{
	public static class CollectionExtensions
	{
		public static IList<string> ToDelimitedList(this string list, string delimiter = ",")
		{
			var delimiters = new[] { delimiter };
			return !string.IsNullOrWhiteSpace(list)
					   ? list.Split(delimiters, StringSplitOptions.RemoveEmptyEntries)
							 .Select(i => i.Trim())
							 .ToList()
					   : new List<string>();
		}

		public static IEnumerable<TItem> ForEach<TItem>(this IEnumerable<TItem> items, Action<TItem> action)
		{
			if (items != null)
			{
				foreach (TItem item in items)
				{
					action(item);
				}
			}

			return items;
		}

		public static TResult[] ForEach<TItem, TResult>(this IEnumerable<TItem> items, Func<TItem, TResult> func)
		{
			var results = new List<TResult>();

			foreach (TItem item in items)
			{
				TResult result = func(item);
				results.Add(result);
			}

			return results.ToArray();
		}

		public static ObservableCollection<TItem> ToObservableCollection<TItem>(this IEnumerable<TItem> items)
		{
			return new ObservableCollection<TItem>(items);
		}
	}
}
